class fenxing:
    def __init__(self):
        self.prebar=None
        self.fx=None
    def oncalc(self,b,timekey=None):
        if self.prebar is not None:
            if self.fx is not None:
                if self.fx<=-1:
                    if b["high"]>self.prebar["high"] and b["low"]>self.prebar["low"]:
                        self.fx=2
                    else:
                        self.fx = -1
                elif self.fx>=1:
                    if b["high"] < self.prebar["high"] and b["low"] < self.prebar["low"]:
                        self.fx = -2
                    else:
                        self.fx = 1
            else:
                if b["high"]>self.prebar["high"] and b["low"]>self.prebar["low"]:
                    self.fx=1
                elif b["high"]<self.prebar["high"] and b["low"]<self.prebar["low"]:
                    self.fx=-1
        self.prebar=b
        return self.fx
class fenxing_box:
    def __init__(self):
        self.prebar=None
        self.fx=None
    def oncalc(self,b,timekey=None):
        if b["open"] > b["close"]:
            low =b["close"]
            high=b["open"]
        else:
            high = b["close"]
            low = b["open"]
        if self.prebar is not None:
            if self.fx is not None:
                if self.fx<=-1:
                    if high>self.prebar[1] and low>self.prebar[0]:
                        self.fx=2
                    elif high>self.prebar[1] and low<self.prebar[0]:
                        delta1=high-self.prebar[1]
                        delta2=self.prebar[1]-self.prebar[0]
                        delta3=self.prebar[0]-low
                        if delta1>delta2 and delta1>delta3 and b["open"]<b["close"]:
                            self.fx=2
                        else:
                            self.fx=-1
                    else:
                        self.fx = -1
                elif self.fx>=1:
                    if high < self.prebar[1] and low < self.prebar[0]:
                        self.fx = -2
                    elif high>self.prebar[1] and low<self.prebar[0]:
                        delta1=high-self.prebar[1]
                        delta2=self.prebar[1]-self.prebar[0]
                        delta3=self.prebar[0]-low
                        if delta1<delta3 and delta2<delta3 and b["close"]<b["open"]:
                            self.fx=-2
                        else:
                            self.fx=1
                    else:
                        self.fx = 1
            else:
                if high>self.prebar[1] and low>self.prebar[0]:
                    self.fx=1
                elif high<self.prebar[1] and low<self.prebar[0]:
                    self.fx=-1
        self.prebar=(low,high)
        return self.fx
class fenxing_byprice:
    def __init__(self,price="close",gap=None):
        self.prebar=None
        self.fx=None
        self.gap=gap
        self.price=price
    def oncalc(self,b,timekey=None):
        if self.prebar is not None:
            t0=b[self.price]
            if self.fx is not None:
                if self.fx<=-1:
                    if self.gap is None:
                        t=self.prebar + b[self.gap]
                    else:
                        t=self.prebar
                    if t0>t:
                        self.fx=2
                    else:
                        self.fx = -1
                elif self.fx>=1:
                    if self.gap is None:
                        t=self.prebar - b[self.gap]
                    else:
                        t=self.prebar
                    if t0 < t:
                        self.fx = -2
                    else:
                        self.fx = 1
            else:
                if t0>self.prebar:
                    self.fx=1
                elif t0<self.prebar:
                    self.fx=-1
        self.prebar=t0
        return self.fx
class fenxing_byclose:
    def __init__(self,gap=0):
        self.prebar=None
        self.fx=None
        self.gap=gap
    def oncalc(self,b,timekey=None):
        if self.prebar is not None:
            if self.fx is not None:
                if self.fx<=-1:
                    if b>self.prebar+self.gap:
                        self.fx=2
                    else:
                        self.fx = -1
                elif self.fx>=1:
                    if b < self.prebar-self.gap:
                        self.fx = -2
                    else:
                        self.fx = 1
            else:
                if b>self.prebar:
                    self.fx=1
                elif b<self.prebar:
                    self.fx=-1
        self.prebar=b
        return self.fx
if __name__ == '__main__':
    from jili.core import load, save
    from research.calcor.calcors_graph import graph_calcor
    k1m = load(r"D:\data\k1m_tq\TA109\202105_k1m.pkl")
    from research.calcor.dim2.cross import cross
    fenxing0=fenxing_byclose()
    cc = [{'calc_cmd': 'ta', 'cmd': 'EMA', 'out': ['ema30'], 'input': {'price': 'close'}, 'arg': {'timeperiod': 30},
           'batch': 30},
          {'calc_cmd': 'ta', 'cmd': 'EMA', 'out': ['ema10'], 'input': {'price': 'close'}, 'arg': {'timeperiod': 10},
           'batch': 10},
          {'calc_cmd': 'function', 'function': fenxing0.oncalc, 'out': 'fenxing0', 'input': ["_"]}]
    c = graph_calcor(cc)
    for d, v in k1m.items():
        for t in v:
            t = c.onbar(t)
    print("end")